﻿using ComponentGIS.Class;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using System;
using System.Windows.Forms;

namespace ComponentGIS.View
{
    public partial class FormAttributeEdit : Form
    {
        #region 字段属性
        IMapControl2 m_pMapControl;
        IEngineEditor m_pEditor;
        IFeatureLayer m_pFeaLyr;
        IFeature m_pFea;
        #endregion
        public FormAttributeEdit(IMapControl2 pMapControl)
        {
            InitializeComponent();
            m_pMapControl = pMapControl;
            m_pEditor = new EngineEditorClass();
            cboLyrName.DataSource = BasicClass.GetFeatureSelectionNameList(pMapControl.Map);
            //cboFeaOID.DataSource = GetOIDs();
        }

        private void cboLyrName_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sFeaLyrName = cboLyrName.SelectedItem.ToString();
            m_pFeaLyr = BasicClass.GetFeatureLayer(m_pMapControl.Map, sFeaLyrName);
            IFeatureSelection pFeaSel = (IFeatureSelection)m_pFeaLyr;
            cboFeaOID.DataSource = BasicClass.GetFeatureLayerOIDList(pFeaSel);

        }

        private void cboFeaOID_SelectedIndexChanged(object sender, EventArgs e)
        {
            //1 清空
            dgvAttributeEdit.Rows.Clear();
            //2 获取要素
            int iSelectedFea = (int)cboFeaOID.SelectedItem;
            m_pFea = m_pFeaLyr.FeatureClass.GetFeature(iSelectedFea);
            //3 填充属性
            for (int i = 0; i < m_pFea.Fields.FieldCount; i++)
            {
                string sFieldName = m_pFea.Fields.Field[i].Name;
                object objFieldValue = m_pFea.Value[i];
                //Shape相关字段不可编辑
                if (!sFieldName.Contains("Shape"))
                    dgvAttributeEdit.Rows.Add(sFieldName, objFieldValue);
            }
            //4 定位要素
            m_pMapControl.Extent = m_pFea.Extent;
            //5 更新【下一个】状态
            if (cboFeaOID.SelectedIndex < cboFeaOID.Items.Count - 1)
                btnNextFea.Enabled = true;
            else
                btnNextFea.Enabled = false;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < dgvAttributeEdit.RowCount; i++)
                {
                    string sFieldName = (string)dgvAttributeEdit.Rows[i].Cells[0].Value;
                    object objFieldValue = dgvAttributeEdit.Rows[i].Cells[1].Value;
                    if (sFieldName != null && sFieldName != string.Empty
                        && !sFieldName.ToUpper().Equals("OBJECTID") && objFieldValue != null)
                    {
                        int index = m_pFea.Fields.FindField(sFieldName);
                        m_pFea.Value[index] = objFieldValue;
                    }
                }
                m_pFea.Store();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnNextFea_Click(object sender, EventArgs e)
        {
            if (cboFeaOID.SelectedIndex < cboFeaOID.Items.Count - 1)
                cboFeaOID.SelectedIndex++;
        }
    }
}
